<template>
  <div v-if="isPageVisable">
  <!--https://element-plus.org/en-US/component/container.html#common-layouts-->
    <el-header style="position:fixed; width:100vw; z-index: 10;">
      <img src='/logo2.png' style="left: -8px; position: relative; height: 60px;"/>
      <table style="height:100%; width: fit-content; float: right;"><tr style="line-height:normal;">
        <td><i class="material-icons icon iconfont" style="font-size:20px;">search</i></td>
        <td><i class="material-icons icon iconfont" style="font-size:20px;">notifications_none</i></td>
        <td>
          <el-dropdown>
            <div class="el-dropdown-link">
              <i class="material-icons icon iconfont" style="font-size:20px;">business_center</i>
              <span style="position: relative; top:-5px; padding-left:8px; font-size:14px;">{{username}}</span>
            </div>
            <template #dropdown >
              <el-dropdown-menu>
                <el-dropdown-item @click="logout" >登出</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </td>
      </tr></table>

      <!--span>{{welcomeWords}}<el-button @click="logout">登出</el-button></span-->
    </el-header>
    <el-container id="el-container-view">
      <el-aside style="width: 200px;">
        <viewSide/>
      </el-aside>
      <el-main id="el-main-view" style="background-color:#f0f2f5; padding-top:20px; ">
        <div style="height: 60px;"></div>
        <perfect-scrollbar>
          <router-view style="overflow-y: scroll; background-color:#f0f2f5;"></router-view>
        </perfect-scrollbar>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import viewSide from "./Aside.vue"

export default ({
  components:{
    viewSide,
  },
  
  data() {
    return{
      welcomeWords: "",
      username: "",
      isPageVisable: false,
    }
  },

  async created(){
    const response= await fetch("http://82.156.0.223:3001/", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        token: sessionStorage.getItem('token')
      }),
    });
    const { auth } = await response.json();
    if (auth==="admin"){
      this.welcomeWords= "欢迎您以管理员的身份登陆！"
      this.username= "admin"
      this.isPageVisable= true;
      console.log("Authentic: "+auth)
    } else if (auth==="user"){
      this.welcomeWords= "欢迎您以普通用户的身份登陆！"
      this.username= "user"
      this.isPageVisable= true;
      console.log("Authentic: "+auth)
    } else {
      this.$router.push("/login");
    }
  },

  methods: {
    async logout(){
      const response= await fetch("http://82.156.0.223:3001/logout", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          token: sessionStorage.getItem('token')
        }),
      });
      this.$router.push("/login");
    }
  },

})
</script>

<style scoped>
.ps{
  height:calc(100vh - 100px); 
}
td{
  padding: 0;
  padding-left: 23px;
}
.el-header, .el-footer {
  color: var(--el-text-color-primary);
  background-color: white;
  text-align: left;
  line-height: 60px;
}
</style>